﻿package bits;

public class FindMaxAmongTwoNums {

	/**
	 * 如果n==1，返回0，如果n==0，返回1
	 * 
	 * @param n
	 * @return
	 */
	public static int flip(int n) {
		return n ^ 1;
	}

	/**
	 * 返回整数n的符号，非负数返回0，负数返回1
	 * 
	 * @param n
	 * @return
	 */

	public static int sign(int n) {
		return (flip(n >> 31) & 1);
	}

	public static int getMax1(int a, int b) {
		int c = a - b;
		int scA = sign(c); // a-b >=0 scA= 0; a-b<0 scA=1;
		int scB = flip(scA);
		return a * scA + b * scB;
	}
	
	
	
	public static int getMax2(int a, int b){
		int c = a-b;
		int sa = sign(a);
		int sb = sign(b);
		int sc= sign(c);
		
		int difSab = sa^sb;
		int sameSab = flip(difSab);
		int returnA = difSab*sa + sameSab*sc;
		int returnB = flip(returnA);
		return a*returnA +b *returnB;
	}

	public static void main(String[] args) {
		System.out.println(getMax1(1, 3));
		System.out.println(getMax2(1, 3));
		
	}
}
